home *** CD-ROM | disk | FTP | other *** search
/ Workbench Add-On / Workbench Add-On - Volume 1.iso / Dev / Amiga-E / E_v3.2a_extras / PdSrc / Talk.e < prev   
Text File  |  1992-09-02  |  6KB  |  151 lines

  1. /* Talk V1.0 - by Rob Verver in 1992                                       */
  2. /*                                                                         */
  3. /* With this shellcommand you can make the narrator say any text using the */
  4. /* new OS2 features. See the helptemplate for more info. When specifying   */
  5. /* a value which is out of range, the correct range will be displayed.     */
  6. /*                                                                         */
  7. /* Possible enhancements:                                                  */
  8. /*   Ability to speak phonetic strings                                     */
  9. /*   Input from standard input, for piping                                 */
  10. /*   Preferences file in ascii format, controling all settings             */
  11. /*   Escape codes changes values halfway a text                            */
  12.  
  13. OPT OSVERSION=37
  14.  
  15. MODULE 'Translator', 'devices/narrator', 'dos/dos', 'exec/memory', 'exec/io'
  16.  
  17. CONST AUDIOCHANSIZE=4
  18. ENUM NONE, ERR_DOS, ERR_MEM, ERR_FILE, ERR_DEVICE, ERR_TRANS, ERR_INVALID
  19.  
  20. OBJECT arglist
  21.   file, rate, pitch, robotic, female, volume, enthusiasm, perturb, f1adj,
  22.   f2adj, f3adj, a1adj, a2adj, a3adj, articulate, centralize, centphon, avbias,
  23.   afbias, priority
  24. ENDOBJECT
  25.  
  26. DEF template, args: arglist, phonebuf=NIL, rdargs=NIL, file=NIL, msgport=NIL,
  27.     filebuf=NIL, ioreq: PTR TO ndi, audiochan: PTR TO CHAR, valid=TRUE,
  28.     length, phonebufsize
  29.  
  30. PROC main () HANDLE
  31.   audiochan := [3, 5, 10, 12]:CHAR
  32.  
  33.   /* parse commandline options */
  34.   args := [NIL, [DEFRATE], [DEFPITCH], 0, 0, [DEFVOL], [DEFF0ENTHUS],
  35.           [DEFF0PERT], [0], [0], [0], [0], [0], [0], [DEFARTIC],
  36.           [DEFCENTRAL], NIL, [0], [0], [25]]: arglist
  37.   template := 'FILE,RATE/K/N,PITCH/K/N,ROBOTIC/S,FEMALE/S,VOLUME/K/N,' + 
  38.               'ENTHUSIASM/K/N,PERTURB/K/N,F1ADJ/K/N,F2ADJ/K/N,F3ADJ/K/N,' +
  39.               'A1ADJ/K/N,A2ADJ/K/N,A3ADJ/K/N,ARTICULATE/K/N,CENTRALIZE/K/N,' +
  40.               'CENTPHON/K,AVBIAS/K/N,AFBIAS/K/N,PRIORITY/K/N'
  41.   rdargs := ReadArgs (template, args, NIL)
  42.   IF rdargs=NIL THEN Raise (ERR_DOS)
  43.  
  44.   /* open translator library */
  45.   translatorbase := OpenLibrary ('translator.library', 37)
  46.   IF translatorbase=NIL THEN Raise (ERR_TRANS)
  47.  
  48.   /* open input file */
  49.   file := Open (args.file, MODE_OLDFILE)
  50.   IF file=NIL THEN Raise (ERR_FILE)
  51.   length := FileLength (args.file)           /* !!! ascii, no fh */
  52.   IF length<1 THEN Raise (ERR_FILE)
  53.  
  54.   /* allocate input buffer */
  55.   filebuf := AllocVec (length, MEMF_PUBLIC)
  56.   IF filebuf=NIL THEN Raise (ERR_MEM)
  57.  
  58.   /* allocate buffer for phonetic strings */
  59.   phonebufsize := Shl (length, 1)
  60.   phonebuf := AllocVec (phonebufsize, MEMF_PUBLIC)
  61.   IF phonebuf=NIL THEN Raise (ERR_MEM)
  62.  
  63.   /* open narrator device */
  64.   msgport := CreateMsgPort ()
  65.   IF msgport=NIL THEN Raise (ERR_DEVICE)
  66.   ioreq := CreateIORequest (msgport, SIZEOF ndi)
  67.   IF ioreq=NIL THEN Raise (ERR_DEVICE)
  68.   ioreq.flags := NDF_NEWIORB
  69.   IF OpenDevice ('narrator.device', 0, ioreq, NIL)<>NIL THEN Raise (ERR_DEVICE)
  70.  
  71.   /* check values validity */
  72.   checkVal (Long (args.rate), MINRATE, MAXRATE, 'Invalid rate')
  73.   checkVal (Long (args.pitch), MINPITCH, MAXPITCH, 'Invalid pitch')
  74.   checkVal (Long (args.volume), MINVOL, MAXVOL, 'Invalid volume')
  75.   checkVal (Long (args.centralize), MINCENT, MAXCENT, 'Invalid centralization')
  76.   IF valid=FALSE THEN Raise (ERR_INVALID)
  77.  
  78.   ioreq.chmasks := audiochan
  79.   ioreq.nummasks := AUDIOCHANSIZE
  80.  
  81.   /* init values */
  82.   ioreq.rate := Long (args.rate)
  83.   ioreq.pitch := Long (args.pitch)
  84.   ioreq.volume := Long (args.volume)
  85.   ioreq.f0enthusiasm := Long (args.enthusiasm)
  86.   ioreq.f0perturb := Long (args.perturb)
  87.   ioreq.f1adj := Long (args.f1adj)
  88.   ioreq.f2adj := Long (args.f2adj)
  89.   ioreq.f3adj := Long (args.f3adj)
  90.   ioreq.a1adj := Long (args.a1adj)
  91.   ioreq.a2adj := Long (args.a2adj)
  92.   ioreq.a3adj := Long (args.a3adj)
  93.   ioreq.articulate := Long (args.articulate)
  94.   ioreq.centralize := Long (args.centralize)
  95.   ioreq.centphon := Long (args.centphon)
  96.   ioreq.avbias := Long (args.avbias)
  97.   ioreq.afbias := Long (args.afbias)
  98.   ioreq.priority := Long (args.priority)
  99.   IF args.robotic<>NIL THEN ioreq.mode := ROBOTICF0 ELSE ioreq.mode := MANUALF0
  100.   IF args.female<>NIL THEN ioreq.sex := FEMALE
  101.  
  102.   process ()
  103.  
  104.   Raise (0)
  105. EXCEPT
  106.   SELECT exception
  107.     CASE ERR_DOS;     PrintFault (IoErr(), 'Error')
  108.     CASE ERR_MEM;     PutStr ('Error: not enough memory\n')
  109.     CASE ERR_FILE;    PutStr ('Error: couldn\at open file\n')
  110.     CASE ERR_DEVICE;  PutStr ('Error: couldn\at open narrator device\n')
  111.     CASE ERR_TRANS;   PutStr ('Error: could\at open translator library V37\n')
  112.     CASE ERR_INVALID; PutStr ('Error: wrong parameters\n')
  113.   ENDSELECT
  114.  
  115.   IF ioreq<>NIL THEN CloseDevice (ioreq) BUT DeleteIORequest (ioreq)
  116.   IF translatorbase<>NIL THEN CloseLibrary (translatorbase)
  117.   IF rdargs<>NIL THEN FreeArgs (rdargs)
  118.   IF phonebuf<>NIL THEN FreeVec (phonebuf)
  119.   IF filebuf<>NIL THEN FreeVec (filebuf)
  120.   IF file<>NIL THEN Close (file)
  121.   IF msgport<>NIL THEN DeleteMsgPort (msgport)
  122.   IF exception THEN CleanUp (10)
  123. ENDPROC
  124.  
  125. PROC checkVal (val, min, max, str)
  126.   IF val<min OR (val>max)
  127.     Vprintf ('%s: valid values are between %ld and %ld\n', [str, min, max])
  128.     valid := FALSE
  129.   ENDIF
  130. ENDPROC
  131.  
  132. PROC process ()
  133.   DEF readlen          /* !!!! was equal to globvar */
  134.  
  135.   readlen := Read (file, filebuf, length)
  136.   IF readlen<>length THEN Raise (ERR_FILE)
  137.  
  138.   Translate (filebuf, length, phonebuf, phonebufsize)
  139.   /* WriteF ('phonetic string:\s\n', phonebuf) */
  140.   speakBuffer (phonebuf, StrLen (phonebuf))
  141. ENDPROC
  142.  
  143. PROC speakBuffer (buffer, length)
  144.   DEF ior:PTR TO iostd
  145.   ior := ioreq
  146.   ior.command := CMD_WRITE
  147.   ior.data := buffer
  148.   ior.length := length
  149.   DoIO (ioreq)
  150. ENDPROC
  151.